//
//  ApproveViewController.m
//  ZJControlsClass
//
//  Created by apple on 2024/10/22.
//

#import "ApproveViewController.h"
#import "PendApprovalViewController.h"
#import "ApprovaledViewController.h"
#import "SendToMeViewController.h"

#import "PendChildCollectionViewCell.h"


@interface ApproveViewController ()<UICollectionViewDelegate,UICollectionViewDataSource
//,protocolRecordViewControllerDelegate
>

@property (nonatomic,strong) UILabel * titileLabel;
@property (nonatomic,strong) UICollectionView * tagCollectionView;
@property (nonatomic,strong) NSMutableArray * tagArr;
@property (nonatomic,strong) NSMutableArray * tagSelectArr;
@property (nonatomic,strong) UICollectionView * collectionView;
@property (nonatomic,strong) NSArray * childDataSource;
@property (nonatomic,strong) PendApprovalViewController *oneVC;
@property (nonatomic,strong) ApprovaledViewController *twoVC;
@property (nonatomic,strong) SendToMeViewController *thereVC;



@end

@implementation ApproveViewController

-(void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    [self.navigationController setNavigationBarHidden:YES animated:NO];
}


- (void)viewDidLoad {
    [super viewDidLoad];
    
    
    self.automaticallyAdjustsScrollViewInsets = NO;
    self.view.backgroundColor = [UIColor whiteColor];
    
    self.tagArr = [NSMutableArray arrayWithObjects:@"待审批",@"已审批",@"抄送我", nil];
    self.tagSelectArr = [NSMutableArray arrayWithObjects:@"1",@"0",@"0", nil];
    
    [self configUI];

}

-(void)viewWillDisappear:(BOOL)animated{
    [super viewWillDisappear:animated];
    [self.navigationController setNavigationBarHidden:NO animated:NO];
}



-(void)configUI{
    
    [self.view addSubview:self.titileLabel];
    [self.view addSubview:self.tagCollectionView];
    [self.view addSubview:self.collectionView];
    
    [self.titileLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.view).with.offset([[UIApplication sharedApplication]statusBarFrame].size.height);
        make.left.right.equalTo(self.view);
        make.height.equalTo(@(44));
    }];
    
    [self.tagCollectionView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.view).offset(StatusBarHeight);
        make.left.right.equalTo(self.view);
        make.height.equalTo(@(ALIGN_HEIGHT_PT(45)));
    }];
        
    [self.collectionView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.bottom.equalTo(self.view);
        make.top.equalTo(self.tagCollectionView.mas_bottom);
    }];
    
}


#pragma mark -- protocolClickButtonChangeCollectionViewContentOffsetDelegate
-(void)ClickButtonWithIndex:(NSInteger)index{
    CGFloat offsetX = index * SCREEN_WIDTH;
    [self.collectionView setContentOffset:CGPointMake(offsetX, 0)];
    
    
    [self.tagSelectArr enumerateObjectsUsingBlock:^(id  _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
        [self.tagSelectArr replaceObjectAtIndex:idx withObject:@"0"];
    }];
    [self.tagSelectArr replaceObjectAtIndex:index withObject:@"1"];
    [self.tagCollectionView reloadData];
    
}

#pragma mark -- UICollectionViewDelegate,UICollectionViewDataSource
-(NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView{
    return 1;
}
-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    if ([collectionView isEqual:self.tagCollectionView]) {
        return self.tagArr.count;
    }
    return self.childDataSource.count;
}


-(__kindof UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    
    if ([collectionView isEqual:self.tagCollectionView]) {
        PendChildCollectionViewCell * cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"PendChildCollectionViewCell" forIndexPath:indexPath];
        
        if ([self.tagSelectArr[indexPath.row] isEqualToString:@"1"]) {
            cell.follewView.backgroundColor = COLOR_RGB(50, 113, 237);
            cell.label.textColor = COLOR_RGB(50, 113, 237);
        }else{
            cell.follewView.backgroundColor = [UIColor clearColor];
            cell.label.textColor = COLOR_RGB(102, 102, 102);
        }
        cell.label.text = [NSString stringWithFormat:@"%@",self.tagArr[indexPath.row]];
        
        return cell;
    }else{
        
        UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:NSStringFromClass([UICollectionViewCell class]) forIndexPath:indexPath];
        UIViewController *vc = self.childDataSource[indexPath.row];
            [cell.contentView addSubview:vc.view];
            [vc.view mas_makeConstraints:^(MASConstraintMaker *make) {
                make.left.right.bottom.top.equalTo(cell.contentView);
            }];
        return cell;
        
    }
    return nil;
}



-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
    
    if ([collectionView isEqual:self.tagCollectionView]) {
        
        CGFloat offsetX = indexPath.row * SCREEN_WIDTH;
        [self.collectionView setContentOffset:CGPointMake(offsetX, 0)];
        
        
        [self.tagSelectArr enumerateObjectsUsingBlock:^(id  _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
            [self.tagSelectArr replaceObjectAtIndex:idx withObject:@"0"];
        }];
        [self.tagSelectArr replaceObjectAtIndex:indexPath.row withObject:@"1"];
        [self.tagCollectionView reloadData];
        
    }
}





#pragma mark - item宽高
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath {
    if ([collectionView isEqual: self.tagCollectionView]) {
        return CGSizeMake(SCREEN_WIDTH / 3, ALIGN_HEIGHT_PT(45));
    }else{
        return self.collectionView.bounds.size;
    }
}

#pragma mark - <UICollectionViewDelegateFlowLayout>
#pragma mark - Y间距
- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout minimumInteritemSpacingForSectionAtIndex:(NSInteger)section {
    return  ALIGN_WIDTH_PT(0);
}
#pragma mark - X间距
- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout minimumLineSpacingForSectionAtIndex:(NSInteger)section {
    return  ALIGN_WIDTH_PT(0);
}


#pragma mark -- 懒加载


-(UILabel *)titileLabel{
    if (!_titileLabel) {
        _titileLabel = [UILabel new];
        _titileLabel.text = @"审批";
        _titileLabel.textColor = [UIColor blackColor];
        _titileLabel.textAlignment = NSTextAlignmentCenter;
        _titileLabel.font = FONT18;
    }
    return _titileLabel;
}


-(UICollectionView *)tagCollectionView{
    if (!_tagCollectionView) {
        UICollectionViewFlowLayout * layout = [[UICollectionViewFlowLayout alloc]init];
        layout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
        _tagCollectionView = [[UICollectionView alloc]initWithFrame:CGRectZero collectionViewLayout:layout];
        _tagCollectionView.delegate = self;
        _tagCollectionView.dataSource = self;
        _tagCollectionView.userInteractionEnabled = YES;
        _tagCollectionView.backgroundColor = [UIColor clearColor];
        _tagCollectionView.showsVerticalScrollIndicator = false;
        _tagCollectionView.showsHorizontalScrollIndicator = false;
        
        // cell
        [_tagCollectionView registerClass:[PendChildCollectionViewCell class] forCellWithReuseIdentifier:@"PendChildCollectionViewCell"];
    }
    return _tagCollectionView;
}



- (UICollectionView *)collectionView{
    if (!_collectionView) {
        UICollectionViewFlowLayout *layout = [UICollectionViewFlowLayout new];
        layout.minimumLineSpacing = 0;
        layout.minimumInteritemSpacing = 0;
        layout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
        _collectionView = [[UICollectionView alloc] initWithFrame:CGRectZero collectionViewLayout:layout];
        _collectionView.delegate = self;
        _collectionView.dataSource = self;
        _collectionView.bounces = NO;
        _collectionView.pagingEnabled = YES;
        _collectionView.scrollEnabled = NO;
        [_collectionView registerClass:[UICollectionViewCell class] forCellWithReuseIdentifier:NSStringFromClass([UICollectionViewCell class])];
        _collectionView.backgroundColor = [UIColor clearColor];
    }
    return _collectionView;
}

- (NSArray *)childDataSource{
    if (!_childDataSource) {
        
        [self addChildViewController:self.oneVC];
        
        [self addChildViewController:self.twoVC];
        
        [self addChildViewController:self.thereVC];
        
        _childDataSource = @[self.oneVC, self.twoVC,self.thereVC];
        
    }
    return _childDataSource;
}


-(PendApprovalViewController *)oneVC{
    if (!_oneVC){
        _oneVC = [PendApprovalViewController new];
    }
    return _oneVC;
}

-(ApprovaledViewController *)twoVC{
    if (!_twoVC){
        _twoVC = [ApprovaledViewController new];
    }
    return _twoVC;
}
-(SendToMeViewController *)thereVC{
    if (!_thereVC){
        _thereVC = [SendToMeViewController new];
    }
    return _thereVC;
}


@end





